package com.itheima.service;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import com.itheima.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class UserService {

    /**
     *
     * 登录方法
     */

    SqlSessionFactory factory =  SqlSessionFactoryUtils.getSqlSessionFactory();


    public User login(String name,String password){

        SqlSession sqlSession = factory.openSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user = mapper.select(name, password);


        sqlSession.close();
        return user;
    }

    /**
     *
     * 登录方法
     */
    public boolean register(User user){

        SqlSession sqlSession = factory.openSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        //判断用户名是否存在
        User u = mapper.selectByUsername(user.getUsername());

        if (u == null) {
            // 用户名不存在,注册
            mapper.add(user);
            sqlSession.commit();
        }

        sqlSession.close();

        return u == null;
    }
}



